package com.qdxht.dispenser.service;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;

import timber.log.Timber;

/**
 * 串口服务帮助类
 * 用于帮助Activity和Fragment更容易地绑定和使用SerialPortService
 * 实现了DefaultLifecycleObserver接口，可以自动管理服务的绑定和解绑
 */
public class SerialPortServiceHelper implements DefaultLifecycleObserver {

    private static final String TAG = "SerialPortServiceHelper";

    private Context context;
    private SerialPortService serialPortService;
    private boolean isBound = false;
    private final ServiceConnectionCallback connectionCallback;

    /**
     * 创建串口服务帮助类
     *
     * @param context 上下文
     * @param callback 服务连接回调
     */
    public SerialPortServiceHelper(Context context, ServiceConnectionCallback callback) {
        this.context = context;
        this.connectionCallback = callback;
    }

    /**
     * 绑定服务
     */
    public void bindService() {
        Timber.tag(TAG).d("绑定串口服务");
        if (!isBound) {
            Intent intent = new Intent(context, SerialPortService.class);
            context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
        }
    }

    /**
     * 解绑服务
     */
    public void unbindService() {
        Timber.tag(TAG).d("解绑串口服务");
        if (isBound) {
            context.unbindService(serviceConnection);
            serialPortService = null;
            isBound = false;
        }
    }

    /**
     * 获取串口服务实例
     */
    public SerialPortService getSerialPortService() {
        return serialPortService;
    }

    /**
     * 检查服务是否已绑定
     */
    public boolean isBound() {
        return isBound && serialPortService != null;
    }

    /**
     * 服务连接
     */
    private final ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Timber.tag(TAG).d("串口服务已连接");
            serialPortService = ((SerialPortService.SerialPortBinder) service).getService();
            isBound = true;
            if (connectionCallback != null) {
                connectionCallback.onServiceConnected(serialPortService);
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Timber.tag(TAG).d("串口服务已断开");
            serialPortService = null;
            isBound = false;
            if (connectionCallback != null) {
                connectionCallback.onServiceDisconnected();
            }
        }
    };

    // 实现DefaultLifecycleObserver接口方法，自动管理服务的绑定和解绑

    @Override
    public void onCreate(LifecycleOwner owner) {
        // 在Activity或Fragment创建时绑定服务
        bindService();
    }

    @Override
    public void onDestroy(LifecycleOwner owner) {
        // 在Activity或Fragment销毁时解绑服务
        unbindService();
    }

    /**
     * 服务连接回调接口
     */
    public interface ServiceConnectionCallback {
        /**
         * 服务连接成功回调
         * @param service 串口服务实例
         */
        void onServiceConnected(SerialPortService service);

        /**
         * 服务断开连接回调
         */
        void onServiceDisconnected();
    }
} 